package com.example.web.demos.crypto;

import org.springframework.security.crypto.codec.Hex;
import org.springframework.security.crypto.codec.Utf8;

/**
 * @author scout.du
 * @date 2024/12/30 14:30
 **/
public class CryptoUtils {

    public static void main(String[] args) {

        System.out.println("===start===");
        String secret = "JsKiVByGHqeD6aPU6";

        String text = "xst_z8dI%6_S669%";

        System.out.println(encode(secret, text));

        System.out.println("===end===");

    }

    public static String encode(String password,String input){

        SecureRandomBytesKeyGenerator bytesKeyGenerator = new SecureRandomBytesKeyGenerator(16);
        AesBytesEncryptor bytesEncryptor = new AesBytesEncryptor(password, "deadbeef", bytesKeyGenerator);

        return new String(Hex.encode(bytesEncryptor.encrypt(Utf8.encode(input))));
    }

    public static String decode(String password,String input){

        SecureRandomBytesKeyGenerator bytesKeyGenerator = new SecureRandomBytesKeyGenerator(16);
        AesBytesEncryptor bytesEncryptor = new AesBytesEncryptor(password, "deadbeef", bytesKeyGenerator);

        return Utf8.decode(bytesEncryptor.decrypt(Hex.decode(input)));
    }
}
